package homework;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DCSGO
 * @version 1.0
 * (1)使用字符流的方式，编写一个客户端程序和服务器端程序
 * (2)客户端发送"name",服务器端接收到后，返回"我是 shroud"
 * (3)客户端发送"hobby"，服务器端接收到后，返回"编写java程序"
 * (4)不是这两个问题，回复"你说啥呢"
 */
public class Homework01Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("正在监听 9999 端口...");
        Socket socketAccept = serverSocket.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(socketAccept.getInputStream()));
        String s = br.readLine();
        System.out.println("收到来自客户端的消息：" + s);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socketAccept.getOutputStream()));
        if (s.equals("name")) {
            bw.write("我是 shroud");
        } else if (s.equals("hobby")) {
            bw.write("编写java程序");
        } else {
            bw.write("你说啥呢");
        }
        bw.newLine();
        bw.flush();
        socketAccept.shutdownOutput();
        System.out.println("回复信息已发送！");

        bw.close();
        socketAccept.close();
        serverSocket.close();

        /*
         * 正在监听 9999 端口...
         * 收到来自客户端的消息：你好！
         * 回复信息已发送！
         *
         * 进程已结束，退出代码为 0
         */
    }
}
